package org.atovio.coolexam.controller.admin;

import org.atovio.coolexam.common.Result;
import org.atovio.coolexam.common.Role;
import org.atovio.coolexam.service.*;
import org.atovio.coolexam.viewmodel.admin.home.CardVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController("AdminHomeController")
@RequestMapping("/admin/home")
public class HomeController {

    private final ExamPaperService examPaperService;
    private final ExamPaperAnswerService examPaperAnswerService;
    private final ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService;
    private final QuestionService questionService;
    private final UserService userService;

    @Autowired
    public HomeController(ExamPaperService examPaperService, ExamPaperAnswerService examPaperAnswerService,
                          ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService,
                          QuestionService questionService, UserService userService) {
        this.examPaperService = examPaperService;
        this.examPaperAnswerService = examPaperAnswerService;
        this.examPaperQuestionCustomerAnswerService = examPaperQuestionCustomerAnswerService;
        this.questionService = questionService;
        this.userService = userService;
    }

    /**
     * 获取表中总记录条数
     * @return 响应结果
     */
    @GetMapping("/card/count")
    public Result getCardCount() {
        CardVM vm = new CardVM();

        vm.setExamPaperCount(examPaperService.getCount(Map.of()));
        vm.setExamPaperAnswerCount(examPaperAnswerService.getCount(Map.of()));
        vm.setExamPaperQuestionCustomerAnswerCount(examPaperQuestionCustomerAnswerService.getCount(Map.of()));
        vm.setQuestionCount(questionService.getCount(Map.of()));
        vm.setTeacherCount(userService.getCount(Map.of("role", Role.T)));
        vm.setStudentCount(userService.getCount(Map.of("role", Role.S)));

        return Result.success(vm);
    }
}
